// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package common
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    public class MoveThumbComponent
    {
        /**
         * 水平
         */
        public static const THUMB_TYPE_HORIZONTAL:int = 1;

        /**
         * 垂直
         */
        public static const THUMB_TYPE_VERTICAL:int = 2;

        private var mLastMousePoint:Point;
        private var mThumbMc:MovieClip;
        private var mThumbType:int;

        private var mOnThumbMoveCallBack:Function;

        public function MoveThumbComponent()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_thumbMc:MovieClip, _thumbType:int, _onThumbMoveCallBack:Function):void
        {
            mThumbMc = _thumbMc;
            mThumbType = _thumbType;
            mOnThumbMoveCallBack = _onThumbMoveCallBack;
            mLastMousePoint = new Point();
            addListener();
        }

        //==================================
        //== Public Function
        //==================================

        public function getValue():int
        {
            if (mThumbType == THUMB_TYPE_HORIZONTAL)
            {
                return  mThumbMc.x + mThumbMc.width / 2;
            }
            else
            {
                return  mThumbMc.y + mThumbMc.height / 2;
            }
        }


        //==================================
        //== Private Function
        //==================================

        private function addListener():void
        {
            mThumbMc.addEventListener(MouseEvent.MOUSE_DOWN, thumbOnMouseDown);
        }


        private function thumbOnMouseDown(e:MouseEvent):void
        {
            mLastMousePoint.x = e.stageX;
            mLastMousePoint.y = e.stageY;

            mThumbMc.stage.addEventListener(MouseEvent.MOUSE_UP, thumbOnMouseUp);
            mThumbMc.stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbOnMouseMove);
        }

        private function thumbOnMouseUp(e:MouseEvent):void
        {
            mThumbMc.stage.removeEventListener(MouseEvent.MOUSE_UP, thumbOnMouseUp);
            mThumbMc.stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbOnMouseMove);
        }

        private function thumbOnMouseMove(e:MouseEvent):void
        {
            //提前取出e.stageX否则当x,y坐标发生偏移以后,再取得的stageX,Y不对
            var offsetX:Number = e.stageX - mLastMousePoint.x;
            var offsetY:Number = e.stageY - mLastMousePoint.y;

            mLastMousePoint.x = e.stageX;
            mLastMousePoint.y = e.stageY;

            if (mThumbType == THUMB_TYPE_HORIZONTAL)
            {
                mThumbMc.x += offsetX;
            } else
            {
                mThumbMc.y += offsetY;
            }
            e.updateAfterEvent();
            mOnThumbMoveCallBack();
        }

    }
}
